package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：wanghao
 * @date ：Created in 2020/12/7
 * @description ：
 * @version: 1.0
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {
        // 1. 角色信息添加
        roleDao.add(role);
        // 2. 角色关联菜单添加
        setRoleAndMenu(role.getId(),menuIds);
        // 3. 角色关联权限添加
        setRoleAndPermission(role.getId(),permissionIds);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findRoleAndMenu(Integer roleId) {
        return roleDao.findRoleAndMenu(roleId);
    }

    @Override
    public List<Integer> findRoleAndPermission(Integer roleId) {
        return roleDao.findRoleAndPermission(roleId);
    }

    @Override
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        // 编辑角色的同时，还需更新角色和菜单还有和权限的关联关系
        // 删除角色和菜单的关联关系
        roleDao.deleteRoleAndMenu(role.getId());
        // 删除角色和权限的关联关系
        roleDao.deleteRoleAndPermission(role.getId());
        // 添加角色和菜单的关联
        setRoleAndMenu(role.getId(),menuIds);
        // 添加角色和权限的关联
        setRoleAndPermission(role.getId(),permissionIds);
        // 更新角色的信息
        roleDao.update(role);
    }

    @Override
    public void deleteById(Integer id) {
        // 删除之前，查看该角色是否有用户关联
        Integer count = roleDao.findCountUserByRoleId(id);
        if (count > 0){
            // 该角色有用户关联，无法删除
            throw new RuntimeException("该角色有用户关联，不能删除");
        }
        roleDao.deleteRoleAndMenu(id);
        roleDao.deleteRoleAndPermission(id);
        roleDao.deleteById(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    // 角色关联菜单添加
    private void setRoleAndMenu(Integer roleId,Integer[] menuIds){
        if (menuIds!=null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    // 角色关联权限添加
    private void setRoleAndPermission(Integer roleId,Integer[] permissionIds){
        if (permissionIds!=null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
}
